package com.cos.hospital.dao;

import android.app.Activity;

import com.cos.hospital.dao.beandao.DbAcupoint;
import com.cos.hospital.dao.beandao.DbCure;
import com.cos.hospital.dao.beandao.DbElement;
import com.cos.hospital.dao.beandao.DbMeridian;
import com.cos.hospital.model.UHttp;
import com.cos.hospital.model.bean.ResultBean;
import com.cos.hospital.model.bean.ResultListBean;
import com.cos.hospital.model.connector.HttpMsgListener;
import com.cos.hospital.model.retrofit.RequestServes;
import com.cos.hospital.tool.SPUtil;
import com.cos.hospital.tool.UToast;
import com.cos.hospital.tool.UTxt;
import com.cos.hospital.widget.ProgressDialog;

import java.util.List;

import io.reactivex.disposables.Disposable;

/**
 * @Description: 校验数据库
 * @Author: Kosmos
 * @Date: 2018年10月17日 22:41
 * @Email: KosmoSakura@foxmail.com
 * @apiNote 1.明天记得催那边一下，最好加两鞭子
 * 2.添加2张表记录
 */
public class UCheckDB {
    private Activity act;
    private RequestServes serves;
    private CheckListener listener;
    private int acu, mer, cure, element;//对应数据库接口状态
    /**
     * init:还没请求
     * fail：请求失败
     * succ：请求成功
     */
    private final int init = 0, fail = 1, succ = 2;
    private int versionCode;//数据库版本号 eg：20181018
//    private boolean isBreak;//是否为正常执行

    public interface CheckListener {
        void onSubscribe(Disposable disposable);

        void onEnd();
    }

    private UCheckDB() {
        acu = init;
        mer = init;
        cure = init;
        element = init;
    }

    public static UCheckDB check(Activity act, RequestServes serves, CheckListener listener) {
        UCheckDB check = new UCheckDB();
        check.act = act;
        check.serves = serves;
        check.listener = listener;
        return check;
    }

    private void onDone() {
        if (acu == init || mer == init || cure == init || element == init) {
            return;
        }

        if (acu == succ && mer == succ && cure == succ && element == succ) {
            ProgressDialog.getInstance().stopProgressDialog();
            SPUtil.saveDBVerison(versionCode);
//            UToast.toast("数据库已更新");
            listener.onEnd();
            return;
        }
        StringBuilder sb = new StringBuilder();
        if (acu == fail) {
            sb.append("穴位");
        }
        if (mer == fail) {
            sb.append(" 经络");
        }
        if (cure == fail) {
            sb.append(" 治疗项目");
        }
        if (element == fail) {
            sb.append(" 元素信息");
        }
        sb.append("数据更新失败");
        ProgressDialog.getInstance().stopProgressDialog();
        UToast.toast(sb.toString());
        listener.onEnd();
    }

    public void checkVersion(String text) {
        ProgressDialog.getInstance().setProgressText(act, text);
        UHttp.start(serves.getCheckDB(""), "校验数据库版本", new HttpMsgListener<ResultBean<Integer>>() {
            @Override
            public void onSubscribe(Disposable disposable) {
                listener.onSubscribe(disposable);
            }

            @Override
            public void onError(String describe) {
                UToast.toast("数据库校验失败");
                listener.onEnd();
            }

            @Override
            public void onSuccess(ResultBean<Integer> dto) {
                versionCode = UTxt.isNull(dto.getResult(), -1);
                if (versionCode < 0) {
                    UToast.toast("数据库校验失败");
                    listener.onEnd();
                    return;
                }
                //保存版本信息
                int dbVersion = SPUtil.getDBVersion();
                if (dbVersion < 0 || dbVersion < versionCode) {
//                    ProgressDialog.getInstance().setProgressText(act, "数据库更新中...");
                    getAcu();//如果穴位有新数据
                    getMer();//如果经络有新数据
                    getCure();//如果治疗项目有新数据
                    getElement();//如果元素信息有新数据
                } else {
                    listener.onEnd();
                }
            }
        });
    }

    /**
     * 获取元素信息
     */
    private void getElement() {
        UHttp.start(serves.getElementDB(""), "获取元素信息", new HttpMsgListener<ResultListBean<DbElement>>() {
            @Override
            public void onSubscribe(Disposable disposable) {
                listener.onSubscribe(disposable);
            }

            @Override
            public void onError(String describe) {
                element = fail;
                onDone();
            }

            @Override
            public void onSuccess(ResultListBean<DbElement> dto) {
                //更新穴位数据
                List<DbElement> result = dto.getResult();
                if (UTxt.isEmpty(result)) {
                    element = fail;
                    onDone();
                } else {
                    HelperElementDb.updateAll(act, result);
                    element = succ;
                    onDone();
                }
            }
        });
    }

    /**
     * 获取穴位数据
     */
    private void getAcu() {
        UHttp.start(serves.getAcupointDB(""), "获取穴位数据", new HttpMsgListener<ResultListBean<DbAcupoint>>() {
            @Override
            public void onSubscribe(Disposable disposable) {
                listener.onSubscribe(disposable);
            }

            @Override
            public void onError(String describe) {
                acu = fail;
                onDone();
            }

            @Override
            public void onSuccess(ResultListBean<DbAcupoint> dto) {
                //更新穴位数据
                List<DbAcupoint> result = dto.getResult();
                if (UTxt.isEmpty(result)) {
                    acu = fail;
                    onDone();
                } else {
                    HelperAcuDb.updateAll(act, result);
                    acu = succ;
                    onDone();
                }
            }
        });
    }

    /**
     * 查询经络数据
     */
    private void getMer() {
        UHttp.start(serves.getMeridianDB(""), "查询经络数据", new HttpMsgListener<ResultListBean<DbMeridian>>() {
            @Override
            public void onSubscribe(Disposable disposable) {
                listener.onSubscribe(disposable);
            }

            @Override
            public void onError(String describe) {
                mer = fail;
                onDone();
            }

            @Override
            public void onSuccess(ResultListBean<DbMeridian> dto) {
                //更新经络数据库
                List<DbMeridian> result = dto.getResult();
                if (UTxt.isEmpty(result)) {
                    mer = fail;
                    onDone();
                } else {
                    HelperMerDb.updateAll(act, result);
                    mer = succ;
                    onDone();
                }
            }
        });
    }

    /**
     * 查询治疗项目数据
     */
    private void getCure() {
        UHttp.start(serves.getCureDB(""), "查询治疗项目数据", new HttpMsgListener<ResultListBean<DbCure>>() {
            @Override
            public void onSubscribe(Disposable disposable) {
                listener.onSubscribe(disposable);
            }

            @Override
            public void onError(String describe) {
                cure = fail;
                onDone();
            }

            @Override
            public void onSuccess(ResultListBean<DbCure> dto) {
                //更新治疗项目数据库
                List<DbCure> result = dto.getResult();
                if (UTxt.isEmpty(result)) {
                    cure = fail;
                    onDone();
                } else {
                    HelperCureDb.updateAll(act, result);
                    cure = succ;
                    onDone();
                }
            }
        });
    }

}
